/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tordroid.core;

import android.os.Environment;
import android.util.Log;
import de.tordroid.model.Profile;
import java.io.*;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.NodeException;

/**
 *
 * @author Christian Simon
 */
public class Database {

    private static String className = "core.db";
    private static File mFile;
    private static final String dbFile = "database.tor";
    private static final String dbLocation = "/Android/data/";
    private static File directory;
    private static final int VERSION = 0;

    private static boolean createDatabase() {
        directory = Environment.getExternalStorageDirectory();

        // Database
        mFile = new File(directory + "/" + dbFile);
        boolean success;
        try {
            if (mFile.createNewFile()) {
                Log.i(className + ".openDatabase()", "Database is empty");
                initializeDatabase();
            } else {
                Log.i(className + ".createdDatabase()", "Using existing database");
            }
            success = true;
        } catch (IOException ex) {
            Log.e(className + ".createDatabase()", "failed creating database");
            success = false;
        }
        return success;
    }

    private static void initializeDatabase() {
        Log.i(className + ".createdDatabase()", "initializing new database");
        writeDatabase(Engine.initProfile());
        Log.i(className + ".createdDatabase()", "New database was created");
    }

    public static void openDatabase() {
        if (createDatabase()) {
            readDatabase();
        } else {
            Log.e(className + ".openDatabase()", "failed opening database");
            Engine.initProfile();
            Log.e(className + ".openDatabase()", "using temp database");
            
        }
    }

    private static void readDatabase() {
        try {
            Reader in = new BufferedReader(new FileReader(mFile));
            Serializer serializer = new Persister();
            Engine.setProfile(serializer.read(Profile.class, in));
            in.close();
        } catch (NodeException ex) {
            Log.i(className + ".readDatabase()", "Database is empty");
            initializeDatabase();
        } catch (Exception ex) {
            Log.e(className + ".readDatabase()", "failed reading database");
        }
    }

    private static void writeDatabase(Profile db) {
        Serializer serializer = new Persister();
        File result = mFile;
        try {
            Log.i(className + ".writeDatabase()", "writing to database");
            serializer.write(db, result);
        } catch (Exception ex) {
            Log.e(className + ".writeDatabase()", "failed writing to database");
        }
    }

    public static void save() {
        Log.i(className + ".save()", "saving current state");
        writeDatabase(Engine.getProfile());
        Log.i(className + ".save()", "current state saved");
    }

    public static boolean removeDatabase() {
        mFile = new File(directory + "/" + dbFile);
        Log.i(className + ".removeDatabase()", "Deleting database file");
        boolean success = mFile.delete();
        return success;
    }
}
